package com.cxk.hr.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.cxk.hr.pojo.Position;
import com.cxk.hr.service.PositionService;
import com.cxk.hr.util.PageSuport;

@Controller
@RequestMapping("position")
public class PositionController {
	@Resource(name = "positionService")
	private PositionService positionService;

	public PositionService getPositionService() {
		return positionService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}

	@RequestMapping("/index")
	public String index() {
		return "admin/index1";
	}

	// 分页查询
	@RequestMapping("/list")
	public String list(Model model) {
		// 总页数
		Integer totalCount = positionService.findCountPosition();
		int currentPageNo = 1;// 当前页
		int pageSize = 5;// 每页的数量
		PageSuport pages = new PageSuport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		List<Position> list = positionService.findPositionByLimit(currentPageNo - 1, pageSize);
		model.addAttribute("list", list);
		model.addAttribute("currentPageNo", currentPageNo);// 当前页
		model.addAttribute("totalPageCount", totalPageCount);// 总页数
		model.addAttribute("totalCount", totalCount);// 记录数
		return "admin/position_list";
	}

	// 前一页
	@RequestMapping("/listPage1")
	public String listPage1(Integer currentPageNo, Model model) {
		// 总页数
		Integer totalCount = positionService.findCountPosition();
		int pageSize = 5;// 每页的数量
		PageSuport pages = new PageSuport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		// 控制首页和尾页
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		List<Position> list = positionService.findPositionByLimit((currentPageNo - 1) * pageSize, pageSize);
		model.addAttribute("list", list);
		model.addAttribute("currentPageNo", currentPageNo);// 当前页
		model.addAttribute("totalPageCount", totalPageCount);// 总页数
		model.addAttribute("totalCount", totalCount);// 记录数
		return "admin/position_list";
	}

	// 后一页
	@RequestMapping("/listPage2")
	public String listPage2(Integer currentPageNo, Model model) {
		// 总页数
		Integer totalCount = positionService.findCountPosition();
		int pageSize = 5;// 每页的数量
		PageSuport pages = new PageSuport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		// 控制首页和尾页
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		List<Position> list = positionService.findPositionByLimit((currentPageNo - 1) * pageSize, pageSize);
		model.addAttribute("list", list);
		model.addAttribute("currentPageNo", currentPageNo);// 当前页
		model.addAttribute("totalPageCount", totalPageCount);// 总页数
		model.addAttribute("totalCount", totalCount);// 记录数
		return "admin/position_list";
	}

	// 到新增页面
	@RequestMapping("/toAdd")
	public String toAdd() {
		return "admin/position_add";
	}

	// 新增
	@RequestMapping("/add")
	public String add(Position position) {
		int count = positionService.addPosition(position);
		return "redirect:/position/list";
	}

	// 到修改界面
	@RequestMapping("/toUpdate")
	public String toUpdate(Integer id, HttpSession session) {
		System.out.println(id);
		Position position = positionService.findPositionById(id);
		session.setAttribute("position", position);
		return "admin/position_update";
	}

	// 修改
	@RequestMapping("/update")
	public String update(Position position, HttpSession session) {
		Position pos = (Position) session.getAttribute("position");
		Integer id = pos.getId();
		position.setId(id);
		int count = positionService.updatePosition(position);
		return "redirect:/position/list";
	}

	// 删除
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(@RequestParam Integer pid) {
		HashMap<String, Integer> resultMap = new HashMap<String, Integer>();
		int count = positionService.deletePosition(pid);
		resultMap.put("count", count);
		return JSONArray.toJSONString(resultMap);
	}
}
